/* utils/tools.js */
import Hashes from '@/assets/js/hashes'

/**
 * 原生桥
 */
export const setWebViewJavascriptBridge = (cb) => {
    function initWebViewJavascriptBridge(){
        if(window.WebViewJavascriptBridge && WebViewJavascriptBridge.init && !WebViewJavascriptBridge._messageHandler){
            // console.log('init--1');
            WebViewJavascriptBridge.init(function(message, responseCallback) {
                // console.log('JS got a message', message);
                var data = {
                    'Javascript Responds': '测试中文!'
                };
                // console.log('JS responding with', data);
                responseCallback(data);
            });
        }
    }

    if (window.WebViewJavascriptBridge) {
        initWebViewJavascriptBridge();
        // console.log('initBefore--');
        return cb(window.WebViewJavascriptBridge);
    }
    document.addEventListener('WebViewJavascriptBridgeReady', function() {
        initWebViewJavascriptBridge();
        cb(WebViewJavascriptBridge);
        // console.log('initBefore--',WebViewJavascriptBridge);
    }, false);
    initWebViewJavascriptBridge();
    if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(cb); }
    window.WVJBCallbacks = [cb];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() { document.documentElement.removeChild(WVJBIframe); }, 0);

};
/**
 * 与原生交互的桥
 */
export const Bridge = (web,native) => {
    // console.log(window.navigator);
    if(window.navigator.userAgent && window.navigator.userAgent.indexOf('zhaoonline') > -1 ){
        if(native && typeof native == 'function'){
            setWebViewJavascriptBridge(function(bridge){
                // console.log('执行bridge',bridge);
                native(bridge);
            });
        }
    }else{
        if(web && typeof web == 'function') web();
    }
};

/**
 * 加密算法
 */
export const Authentication = function(appId, key, deviceId, HttpMothod, HttpPath) {
    return 'ZHAO ' + appId + ':' + new Hashes.SHA1().b64_hmac(key, HttpMothod.toUpperCase() + '\n\n' + HttpPath + '\n' + deviceId);
};

/**
 * app类型
 */
export const getPlatform = () => {
    var result = {
        TYPE: '',
        APP: ''
    }
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        result.TYPE = 'ios'
    } else if (/(Android)/i.test(navigator.userAgent)) {
        result.TYPE = 'android'
    } else {
        result.TYPE = 'pc'
    }
    if (window.navigator.userAgent.indexOf('zhaoonline') > -1){
        result.APP = 'zhaoonline'
    } else  if (window.navigator.userAgent.toLowerCase().indexOf('micromessenger') > -1) {
        result.APP = 'weixin'
    } else  if (window.navigator.userAgent.toLowerCase().indexOf('weibo') > -1) {
        result.APP = 'weibo'
    } else {
        result.APP = 'default'
    }
    return result
}

/**
 * 获取url参数
 */
export const getQueryString = (name) => {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
        var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

/**
 * 判断字符串是否包含表情符号
 */
export const isEmojiCharacter = (data) => {
    for ( var i = 0; i < data.length; i++) {
        var hs = data.charCodeAt(i);
        if (0xd800 <= hs && hs <= 0xdbff) {
            if (data.length > 1) {
                var ls = data.charCodeAt(i + 1);
                var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                if (0x1d000 <= uc && uc <= 0x1f77f) {
                    return true;
                }
            }
        } else if (data.length > 1) {
            var ls = data.charCodeAt(i + 1);
            if (ls == 0x20e3) {
                return true;
            }
        } else {
            if (0x2100 <= hs && hs <= 0x27ff) {
                return true;
            } else if (0x2B05 <= hs && hs <= 0x2b07) {
                return true;
            } else if (0x2934 <= hs && hs <= 0x2935) {
                return true;
            } else if (0x3297 <= hs && hs <= 0x3299) {
                return true;
            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030
                    || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b
                    || hs == 0x2b50) {
                return true;
            }
        }
    }
}